package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.readAndParseXML.service.IHwShljqsService;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.smartformCsdlwj.service.ISmartFormShljqsService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.ShljqsForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
/**
 * 解析生活垃圾清扫xml数据  陆域
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class HwShljqsServiceImpl implements IHwShljqsService {
	Logger logger = (Logger) LoggerFactory.getLogger(HwShljqsServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

//	@Autowired
//	private IYwshljqssjSqbService ywshljqssjSqbService;
	
	@Autowired
	private ISmartFormShljqsService smartFormShljqsService;
	
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 

	// xml存放路径
	private final String shljqssjfilepath = localResource.getString("shljqssjfilepath");

	// 附件存放路径
	private final String shljqssjfujianpath = localResource.getString("shljqssjfujianpath");

	// 解析成功反馈xml存放路径
	private final String shljqssjfeedbackPath = localResource.getString("shljqssjfeedbackPath");

	// 解析异常xml存放路径
	private final String shljqssjexceptionPath = localResource.getString("shljqssjexceptionPath");

	// 解析后，xml备份
	private final String shljqssjbackPath = localResource.getString("shljqssjbackPath");

	private Map<String, String> shljqssjKeyMap;// xml内容的key

	private Map<String, String> shljqssjKeyMapHead;// xml头部的key

	private Map<String, Long> shljqssjfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initshljqssjMap() {
		// 初始化数据部分对应的map
		shljqssjKeyMap = new HashMap<String, String>();
		shljqssjKeyMap.put("YSL_NO", "YSL_NO");
		shljqssjKeyMap.put("approvalNo", "approvalNo");
		shljqssjKeyMap.put("businessCode", "businessCode");
		shljqssjKeyMap.put("sendTime", "sendTime");
		
		shljqssjKeyMap.put("TBRQ", "TBRQ");
//		shljqssjKeyMap.put("FWNRQS", "FWNRQS");
//		shljqssjKeyMap.put("FWNRSJ", "FWNRSJ");
//		shljqssjKeyMap.put("FWNRYS", "FWNRYS");
		shljqssjKeyMap.put("QYMC", "QYMC");
		shljqssjKeyMap.put("ZZJGDM", "ZZJGDM");
		shljqssjKeyMap.put("FRDB", "FRDB");
		shljqssjKeyMap.put("YYZZZCH", "YYZZZCH");
		shljqssjKeyMap.put("LXDH", "LXDH");
		shljqssjKeyMap.put("CZDH", "CZDH");
		shljqssjKeyMap.put("EMAIL", "EMAIL");
		shljqssjKeyMap.put("YZBM", "YZBM");
		shljqssjKeyMap.put("BGZS", "BGZS");//新增这个  -2.20
		shljqssjKeyMap.put("ZCZB", "ZCZB");
		shljqssjKeyMap.put("XMMC", "XMMC");
		shljqssjKeyMap.put("WTS", "WTS");
		shljqssjKeyMap.put("LXDH2", "LXDH2");
		shljqssjKeyMap.put("CZDH2", "CZDH2");
		shljqssjKeyMap.put("EMAIL2", "EMAIL2");
		shljqssjKeyMap.put("BZ", "BZ");
		shljqssjKeyMap.put("AREA_CODE", "AREA_CODE");
		shljqssjKeyMap.put("SQFWNR", "SQFWNR");
		// 初始化头部对应的map
		shljqssjKeyMapHead = new HashMap<String, String>();
		shljqssjKeyMapHead.put("sn", "sn");
		shljqssjKeyMapHead.put("sender", "sender");
		// shljqssjKeyMapHead.put("receiver", "receiver");
		shljqssjKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		shljqssjfujianMap = new HashMap<String, Long>();
		shljqssjfujianMap.put("FJ_01", 363l);
		shljqssjfujianMap.put("FJ_02", 360l);
		shljqssjfujianMap.put("FJ_03", 364l);
		shljqssjfujianMap.put("FJ_04", 365l);
		shljqssjfujianMap.put("FJ_05", 420l);
		shljqssjfujianMap.put("FJ_06", 379l);
		shljqssjfujianMap.put("FJ_07", 499l);
		shljqssjfujianMap.put("FJ_08", 500l);
		shljqssjfujianMap.put("FJ_09", 501l);
		shljqssjfujianMap.put("FJ_10", 502l);
		shljqssjfujianMap.put("FJ_11", 503l);
	}

	public void readXml(String projectPath) {
		if (shljqssjKeyMap == null)
			this.initshljqssjMap();
		File file = new File(shljqssjfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(shljqssjKeyMap, filePath,shljqssjfeedbackPath, shljqssjexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(shljqssjKeyMapHead,files[i], shljqssjfeedbackPath, shljqssjexceptionPath);

						// 把数据部分保存到数据库
						ShljqsForm form = new ShljqsForm();
						for (String mapKey : shljqssjKeyMap.keySet()) {							
							// TODO 把值放到form中
							form.setQymc(parseXMLService.getMapValue(shljqssjKeyMap.get("QYMC")));
							
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(shljqssjKeyMap.get("QYMC")),"shljqssj");
							if(ywComQyxxForm != null){
								form.setQybh(ywComQyxxForm.getId().toString());
							}
							
//							form.setFwnrqs(parseXMLService.getMapValue(shljqssjKeyMap.get("FWNRQS")));
//							form.setFwnrsj(parseXMLService.getMapValue(shljqssjKeyMap.get("FWNRSJ")));
//							form.setFwnrys(parseXMLService.getMapValue(shljqssjKeyMap.get("FWNRYS")));
							form.setZzjgdm(parseXMLService.getMapValue(shljqssjKeyMap.get("ZZJGDM")));
							form.setFrdb(parseXMLService.getMapValue(shljqssjKeyMap.get("FRDB")));
							form.setYyzzzch(parseXMLService.getMapValue(shljqssjKeyMap.get("YYZZZCH")));
							form.setLxdh(parseXMLService.getMapValue(shljqssjKeyMap.get("LXDH")));
							form.setCzdh(parseXMLService.getMapValue(shljqssjKeyMap.get("CZDH")));
							form.setEmail(parseXMLService.getMapValue(shljqssjKeyMap.get("EMAIL")));
							form.setYzbm(parseXMLService.getMapValue(shljqssjKeyMap.get("YZBM")));
							form.setBgzs(parseXMLService.getMapValue(shljqssjKeyMap.get("BGZS")));//新增这个  - 2.20
							form.setZczb(parseXMLService.getMapValue(shljqssjKeyMap.get("ZCZB")));
							form.setXmmc(parseXMLService.getMapValue(shljqssjKeyMap.get("XMMC")));
							form.setWts(parseXMLService.getMapValue(shljqssjKeyMap.get("WTS")));
							form.setLxdh2(parseXMLService.getMapValue(shljqssjKeyMap.get("LXDH2")));
							form.setCzdh2(parseXMLService.getMapValue(shljqssjKeyMap.get("CZDH2")));
							form.setEmail2(parseXMLService.getMapValue(shljqssjKeyMap.get("EMAIL2")));
							form.setBz(parseXMLService.getMapValue(shljqssjKeyMap.get("BZ")));
							form.setAreaCode(parseXMLService.getMapValue(shljqssjKeyMap.get("AREA_CODE")));	
							String sqfwnr = parseXMLService.getMapValue(shljqssjKeyMap.get("SQFWNR"));
//							String sqfwnr = "传过来的值";
							if(StringUtils.isBlank(sqfwnr) || "null".equals(sqfwnr)){//为空，或者为null
								form.setFwnrqs("1");//没选择
								form.setFwnrsj("1");//没选择
								form.setFwnrys("1");//没选择
							}else{//有值而且不为  "null"
								if(sqfwnr.contains("1")){//选择了清扫
									form.setFwnrqs("0");//选择
								}else{
									form.setFwnrqs("1");//没选择
								}
								if(sqfwnr.contains("2")){//选择了收集
									form.setFwnrsj("0");//选择
								}else{
									form.setFwnrsj("1");//没选择
								}
								if(sqfwnr.contains("3")){//选择了运输
									form.setFwnrys("0");//选择
								}else{
									form.setFwnrys("1");//没选择
								}								
							}
							
							
							String TBRQTemp = parseXMLService.getMapValue(shljqssjKeyMap.get("TBRQ"));
							Date TBRQ = new Date();
							if (org.apache.commons.lang.StringUtils.isNotEmpty(TBRQTemp)) {
								TBRQ = DateUtils.stringToDate(TBRQTemp,DateUtils.YYYY_MM_DD);
							}
							form.setTbrq(TBRQ);
							form.setYslNo(parseXMLService.getMapValue(shljqssjKeyMap.get("YSL_NO")));
							form.setApprovalNo(parseXMLService.getMapValue(shljqssjKeyMap.get("approvalNo")));							
							form.setBusinessCode(parseXMLService.getMapValue(shljqssjKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(shljqssjKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setSendNotice("Y");
//							form.setYsl(1l);
							
							form.setCbbm("环卫处");
							form.setCbyw("从事陆域城市生活垃圾经营性清扫、收集、运输服务");
						}
						// 调用DAO，保存到数据库
						try {
							smartFormShljqsService.save(form);
							smartFormShljqsService.autoStartProc(form, "shljqssj");
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									shljqssjKeyMapHead, shljqssjfeedbackPath, shljqssjexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, shljqssjfujianpath, shljqssjfeedbackPath, shljqssjexceptionPath, shljqssjKeyMapHead,
								files, shljqssjfujianMap, i, Integer.parseInt(form.getQybh()), form.getId(), "shljqssj");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								shljqssjKeyMapHead, shljqssjfeedbackPath, shljqssjexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, shljqssjbackPath + fileName,
							fileName);
				}
			}
		}
	}

}
